/*
 * @(#)SimFrameRIP.java	  06.09.1997  Didier Gutacker
 * 
 */

import java.awt.*;
import java.lang.*;

/** 
 * Die Klasse SimFrameRIP stellt die graphische Schnittstelle der Simulation des
 * RIP-Routing-Algorithmus zum Benutzer dar. 
 *
 * @author 	Didier Gutacker
 * @version     1.0, 06.09.1997
 * @see ScrolledNetworkCanvasRIP
 * @see IndexAreaRIP
 * @see ButtonPanelRIP
 */

/**
 *
 * Changes / Enhancements :
 *
 * 1) replaced system.exit(0) by this.hide() and this.dispose() to avoid
 *    problems closing the Window in Win 95 
 * 
 * 2) class SimFrameRIP ist Oberflaeche des RIP-Routing-Applets
 *
 * Changes made by Didier Gutacker in August 1997
 *
 */

public final class SimFrameRIP extends SimulationFrame {

  public ScrolledNetworkCanvasRIP canvasArea; 
  public ButtonPanelRIP buttons;
  private MenuItem periodicUpdateSenderMenuItem; 

  /**
   * Konstruktor im Falle wenn die Simulation als Applet laeuft
   * @see SimulationApplet
   */

  public SimFrameRIP( TextMethods text, Simulation simulation ) {
    this.text = text;
    this.simulation = simulation;
    if ( text instanceof TextMethodsEnglish ) {
      english = true;
    } else {
      english = false;
    } // if

   this.tg = simulation.getThreadGroup();

    // title
    setTitle( text.frameTitle );
    
    // menuBar
    MenuBar menuBar = new MenuBar();
      
    // window menu
    windowMenu = new Menu( text.menuBar_windowButtonText );
    newMenuItem = new MenuItem( text.menuBar_editButton_newButtonText );
    windowMenu.add( newMenuItem );
    windowMenu.addSeparator();
    quitMenuItem = new MenuItem( text.menuBar_windowButton_QuitButtonText );
    windowMenu.add( quitMenuItem );
    
    // edit menu
    editMenu = new Menu( text.menuBar_editButtonText );
    simulationSpeedMenu= new Menu( text.menuBar_editButton_simulationSpeedButtonText );
    simulationSpeedSlowMenuItem = new MenuItem( text.menuBar_editButton_simulationSpeedButton_slowButtonText );
    simulationSpeedNormalMenuItem = new MenuItem( text.menuBar_editButton_simulationSpeedButton_normalButtonText );
    simulationSpeedFastMenuItem = new MenuItem( text.menuBar_editButton_simulationSpeedButton_fastButtonText );
    simulationSpeedMenu.add( simulationSpeedSlowMenuItem );
    simulationSpeedMenu.add( simulationSpeedNormalMenuItem );
    simulationSpeedMenu.add( simulationSpeedFastMenuItem );
    editMenu.add( simulationSpeedMenu );
    changeLanguageMenuItem = new MenuItem( text.menuBar_windowButton_ChangeLanguageButtonText );
    editMenu.add( changeLanguageMenuItem );
    editMenu.addSeparator();
    periodicUpdateSenderMenuItem = new MenuItem ( text.menuBar_editButton_updateSenderText1 );
    editMenu.add( periodicUpdateSenderMenuItem );
    editMenu.addSeparator();
    speechLanguageMenuItem = new MenuItem( text.menuBar_windowButton_SpeechLanguageButtonText);
    editMenu.add( speechLanguageMenuItem );
    editMenu.addSeparator();
    startDemoMenuItem = new MenuItem( text.menuBar_editButton_startDemoButtonText );
    editMenu.add( startDemoMenuItem );
      
    // help menu
    helpMenu = new Menu( text.menuBar_helpButtonText );
    infoMenuItem = new MenuItem( text.menuBar_helpButton_InfoButtonText );
    helpMenu.add( infoMenuItem );
    helpMenu.addSeparator();
    introductionMenuItem = new MenuItem( text.menuBar_helpButton_IntroductionButtonText );
    helpMenu.add( introductionMenuItem );
    
    menuBar.add( windowMenu );
    menuBar.add( editMenu );
    menuBar.add( helpMenu );
    setMenuBar( menuBar );
    
    // main layout
    
    setLayout( new BorderLayout() );
    
    // upperRow 
    
    upperRow = new Panel();
    upperRow.setLayout( new GridLayout( 1, 1 ) ); 
    add( "Center", upperRow );
    
    // lowerRow 
      
    lowerRow = new Panel();
    lowerRow.setLayout( new BorderLayout() );
    add( "South", lowerRow );
    
    // text area
      
    textArea = new TextArea( ROWS, COLS );
    textArea.setFont( new Font( "TimesRoman", Font.BOLD, 12 ) );
    textArea.setEditable( false );
    lowerRow.add ( "South", textArea );
    
    Panel lowerRowPanel = new Panel();
    lowerRowPanel.setLayout( new GridLayout( 1, 2 ) );

    // button area
      
    buttons = new ButtonPanelRIP( text );
    lowerRowPanel.add( buttons );

    // index area
    
    index = new IndexAreaRIP( simulation, this );
    lowerRowPanel.add( index );

    lowerRow.add( "Center", lowerRowPanel );
    
    // canvas area 
        
    canvasArea = new ScrolledNetworkCanvasRIP( simulation, this );
    upperRow.add( canvasArea );
    upperRow.layout();
    buttons.initialize( ((NetworkCanvasRIP)canvasArea.getCanvas()).getNetwork() );
    buttons.layout();
    
    lowerRow.layout();
    initialized = true;
    repaint();
  } // SimFrameRIP.SimFrameRIP


  /**
   * Konstruktor im Falle wenn die Simulation als Client laeuft
   * @see SimulationClient
   */

  public SimFrameRIP( TextMethods text, Simulation simulation, SimClientRIP client ) {
    this( text, simulation );
    isClient = true;
    this.client = client;
    text.frameTitle += " <CLIENT>";
    editMenu.disable();
    buttons.disableAll();
    canvasArea.startRemote();
    repaint();
  } // SimFrameRIP

  /**
   * Konstruktor im Falle wenn die Simulation als Server läuft
   * @see SimulationServer
   */

  public SimFrameRIP( TextMethods text, Simulation simulation, SimServerRIP server ) {
    this( text, simulation );
    isServer = true;
    this.server = server;
    text.frameTitle += " <SERVER>";
    repaint();
  } // SimFrameRIP

   public void paint(Graphics gc) {
     super.paint(gc);
     buttons.repaint();
     canvasArea.repaint();
  }

  public boolean action( Event e, Object arg ) {
    if ( arg == null ) return true;
    if ( arg.equals( text.menuBar_windowButton_QuitButtonText ) ) {
      canvasArea.finishUp();
      dispose();
      canvasArea = null;
      upperRow = null;
      lowerRow = null;
      index = null;
      buttons = null;
      return true;
    } // if

    if ( arg.equals( text.menuBar_editButton_newButtonText ) ) {
      canvasArea.finishUp();
      redrawLabels();
      return true;
    } // if
    
    if ( arg.equals( text.menuBar_editButton_simulationSpeedButton_slowButtonText ) ) {
      canvasArea.setAnimationSpeed( 0 );
      setAnimationSpeed( 0 );
      index.repaint();
      return true;
    } // if

    if ( arg.equals( text.menuBar_editButton_simulationSpeedButton_normalButtonText ) ) {
      canvasArea.setAnimationSpeed( 1 );
      setAnimationSpeed( 1 );
      index.repaint();
      return true;
    } // if

    if ( arg.equals( text.menuBar_editButton_simulationSpeedButton_fastButtonText ) ) {
      canvasArea.setAnimationSpeed( 2 );
      setAnimationSpeed( 2 );
      index.repaint();
      return true;
    } // if

    if ( arg.equals( text.menuBar_editButton_updateSenderText1 ) ) {
      periodicUpdateSenderMenuItem.setLabel( text.menuBar_editButton_updateSenderText2 );
      canvasArea.change_noupdatesending();
    } // if

     if ( arg.equals( text.menuBar_editButton_updateSenderText2 ) ) {
      periodicUpdateSenderMenuItem.setLabel( text.menuBar_editButton_updateSenderText1 );
      canvasArea.change_noupdatesending();
    } // if

    if ( arg.equals( text.menuBar_editButton_startDemoButtonText ) ) {
      canvasArea.startDemo();
      return true;
    } // if

    if ( arg.equals( text.menuBar_helpButton_InfoButtonText ) ) {
      InfoWindow infoWindow = new InfoWindow( text.appletText + " Info" );
      infoWindow.expose( text.infoText );
    } // if

    if ( arg.equals( text.menuBar_helpButton_IntroductionButtonText ) ) {
      InfoWindow infoWindow = new InfoWindow( text.helpText1 );
      infoWindow.expose( text.helpText2 );
    } // if

     if ( arg.equals( text.menuBar_windowButton_ChangeLanguageButtonText ) ) {
      if ( english ) {
	english = false;
	text = new TextMethodsDeutschRIP();
      } else {
	english = true;
	text = new TextMethodsEnglishRIP();
      } // if 
      redrawLabels();
      return true;
     } // if
    return false;
  } // SimFrameRIP.action

   void redrawLabels() {
     if ( isClient() ) {
      buttons.disableAll();
    } else {
      // propagate to children
      buttons.redrawLabels( text );
    } // if
    super.redrawLabels();
  } // SimFrameRIP.redrawLabels

  public final void resetButtons() {
    if ( !isClient() ) {
      buttons.resetState();
    } // if
  } // SimFrameRIP.resetButtons

} // SimFrameRIP  



